home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 400_01 / socketpp-1.5 / test / tsmtp.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  1.4 KB  |  63 lines

  1. #include <sockinet.h>
  2.  
  3. extern "C" int sprintf(char*, const char*, ...);
  4.  
  5. static void send_cmd(iosockstream&, const char* cmd=0);
  6. static void get_text(iosockstream&);
  7.  
  8. main(int ac, char** av)
  9. {
  10.     if (ac != 3) {
  11.     cerr << "USAGE: " << av[0] << " hostname userid\n";
  12.     return 1;
  13.     }
  14.     
  15.     sockinetaddr sina(av[1], "smtp", "tcp");
  16.     iosockinet   sio (sockbuf::sock_stream);
  17.     
  18.     sio->connect(sina);
  19.     
  20.     send_cmd(sio, 0);
  21.     send_cmd(sio, "HELO kelvin.seas.virginia.edu");
  22.     send_cmd(sio, "HELP");
  23.     send_cmd(sio, "MAIL FROM:<test@test>");
  24.     
  25.     char buf[512];
  26.     sprintf(buf, "RCPT TO:%s", av[2]);
  27.     send_cmd(sio, buf);
  28.     
  29.     send_cmd(sio, "DATA");
  30.     cout << "terminate you input with cntrl-D\n\n";
  31.     while(cin.getline(buf, 511)) {
  32.     if (buf[0] == '.') sio << '.';
  33.     sio <<  buf << "\r\n";
  34.     }
  35.     sio << "\r\n.\r\n";
  36.     send_cmd(sio, 0);
  37.     
  38.     
  39.     send_cmd(sio, "HELP");
  40.     
  41.     send_cmd(sio, "QUIT");
  42. }
  43.  
  44. void send_cmd(iosockstream& s, const char* cmd)
  45. {
  46.     if (cmd) {
  47.     s << cmd << "\r\n";
  48.     }
  49.     get_text (s);
  50. }
  51.  
  52. void get_text(iosockstream& s)
  53. {
  54.     char buf[256];
  55.     int tmo = s->recvtimeout(1); // wait for 1 sec before timing out
  56.     while ( s.getline(buf, 255) ) {
  57.     if (buf[0] == '.' && buf[1] == '\r' && buf[2] == '\n') break;
  58.         cout << buf << endl;
  59.     }
  60.     if ( !s->is_eof() ) s.clear();
  61.     s->recvtimeout(tmo); //reset timeout to the previous value
  62. }
  63.